<?php namespace App\Http\Requests;

use App\Http\Requests\Request;

class UserRequest extends Request
{

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }


    public function rules()
    {
        $rule = [
            'name' => 'required|min:2|max:60',
            'email' => 'required|email|unique:users,email',
            'phone' => 'required|size:11',
            'password' => 'required|min:6|max:16|regex:/^[a-zA-Z0-9~@#%_]{6,16}$/i',
            'password_confirmation' => 'required|same:password',
            'old_password' => 'required',
            'role_id' => 'required|exists:roles,id',
        ];
        $rules = [];
        switch ($this->path()) {
            case 'account/login' :
                $rules = [
                    'email' => $rule['email'],
                    'password' => $rule['password'],
                ];
                break;
            case 'account/register' :
                $rules = [
                    'name' => $rule['name'],
                    'email' => $rule['email'],
                    'password' => $rule['password'],
                    'password_confirmation' => $rule['password_confirmation']
                ];
                break;
            case 'user/change-password' :
                $rules = [
                    'old_password' => $rule['old_password'],
                    'password' => $rule['password'],
                    'password_confirmation' => $rule['password_confirmation'],
                ];
                break;
            case 'user/edit' :
                $rules = [
                    'name' => $rule['name'],
                    'phone' => $rule['phone'],
                ];
                break;
            case 'admin/user/edit' :
                $rules = [
                    'name' => $rule['name'],
                    'phone' => $rule['phone'],
                    'role_id' => $rule['role_id'],
                ];
                break;
            case 'admin/user/add' :
                $rules = [
                    'name' => $rule['name'],
                    'email' => $rule['email'],
                    'phone' => $rule['phone'],
                    'role_id' => $rule['role_id'],
                    'password' => $rule['password']
                ];
                break;
        }
        return $rules;
    }

    /**
     * 自定义验证信息
     *
     * @return array
     */
    public function messages()
    {
        return [
            'name.required' => '请填写真实姓名',
            'name.min' => '姓名字数不得少于2',
            'name.max' => '姓名字数不得多于60',

            'email.required' => '请填写邮箱地址',
            'email.email' => '请填写正确合法的邮箱地址',
            'email.unique' => '此邮箱地址已存在于系统，不能再进行二次关联',

            'phone.required' => '请填写手机号码',
            'phone.size' => '国内的手机号码长度为11位',
            'phone.mobile_phone' => '请填写合法的手机号码',
            'phone.unique' => '此手机号码已存在于系统中，不能再进行二次关联',

            'age.required' => '请填写年龄',
            'age.numeric' => '年龄必须为数字',
            'age.between' => '年龄在1-120之内',

            'address.required' => '请填写地址',

            'password.required' => '请填写登录密码',
            'password.min' => '密码长度不得少于6',
            'password.max' => '密码长度不得超出16',
            'password.regex' => '密码包含非法字符，只能为英文字母(a-zA-Z)、阿拉伯数字(0-9)与特殊符号(~@#%_)组合',
            'password_confirmation.required' => '请填写确认密码',
            'password_confirmation.same' => '2次密码不一致',
            'old_password.required' => '请填写密码',

            'role_id.required' => '请选择角色（用户组）',
            'role_id.exists' => '系统不存在该角色（用户组）',
        ];
    }

}
